﻿using AutoMapper;
using GGGK.Film.IService;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using GGGK.Film.Models.Models;
using Microsoft.AspNetCore.Mvc;

namespace GGGK.Film.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UserRoleController : ControllerBase
    {
        private readonly IUserRoleService svc;
        private readonly IMapper mapper;

        public UserRoleController(IUserRoleService svc, IMapper mapper)
        {
            this.svc = svc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据用户id获取
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("{userId}")]
        public Result<IEnumerable<Base_UserRole>> Get(int userId)
        {
            var data = svc.Query(o => o.UserId == userId && o.Status == 1);

            return new Result<IEnumerable<Base_UserRole>> { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 授权
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(UserRoleBatchDto dto)
        {
            var data = dto.RoleIds.Select(o => new Base_UserRole()
            {
                UserId = dto.UserId,
                RoleId = o,
                Status = 1,
                CreateTime = DateTime.Now,
            });

            if (svc.Add(dto.UserId, data))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }


    }
}
